""" 
步进电机控制 

危险提示:
    暂停程序之后, 要立刻断开电源, 否则电机可能过热损坏
    原因: 停止程序时, 停留在最后一步, 莫一路IO输出高, 电机一直处于通电状态
    解决方法: 看程序 10.2_key_control_setup_motor.py 使用按键控制电机
"""

#导入Pin模块
from machine import Pin
import time

# 步进电机引脚定义
motor_a = Pin(15, Pin.OUT, Pin.PULL_DOWN)
motor_b = Pin(2, Pin.OUT, Pin.PULL_DOWN)
motor_c = Pin(0, Pin.OUT, Pin.PULL_DOWN)
motor_d = Pin(4, Pin.OUT, Pin.PULL_DOWN)

def setup_motor_send_pulse(setup):
    if setup == 1:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(0)
    elif setup == 2:
        motor_a.value(1)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif setup == 3:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(0)
        motor_d.value(0)
    elif setup == 4:
        motor_a.value(0)
        motor_b.value(1)
        motor_c.value(1)
        motor_d.value(0)
    elif setup == 5:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(0)
    elif setup == 6:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(1)
        motor_d.value(1)
    elif setup == 7:
        motor_a.value(0)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)
    elif setup == 8:
        motor_a.value(1)
        motor_b.value(0)
        motor_c.value(0)
        motor_d.value(1)

if __name__ == "__main__":
    speed = 1
    setup = 1
    while True:
        setup_motor_send_pulse(setup)
        time.sleep_ms(speed)
        setup += 1
        if setup > 8:
            setup = 1